<template>
  <div class="cus">
    <div class="cus_box" style="width:500px;">
      <div class="title">
        <span>Import</span
        ><i @click="$emit('uploadFile_call')" class="el-icon-close"></i>
      </div>
      <div class="content" style="text-align: center">
        <el-upload
          class="upload-demo"
          ref="upload"
          :headers="headers"
          :action="action"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-success="on_success"
          :file-list="fileList"
          :auto-upload="false"
        >
          <el-button slot="trigger" size="small" type="primary"
            >Select File</el-button
          >
          <!--                <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>-->
          <div slot="tip" class="el-upload__tip">
            Only xlsx and xls formats can be uploaded
          </div>
        </el-upload>
      </div>
      <div style="text-align: center" class="mt20">
        <el-button @click="$emit('uploadFile_call')">Cancel</el-button>
        <el-button type="primary" @click="submitUpload">Confirm</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "uploadFile",
  props: {
    type: [String]
  },
  data() {
    return {
      fileList: [
        // {name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'},
        // {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}
      ]
    };
  },
  computed: {
    action() {
      if (this.type === "ic") {
        if (process.env.NODE_ENV !== "production") {
          return `${process.env.VUE_APP_URL}/web/ic/importData`;
        } else {
          return process.env.VUE_APP_URL + "/web/ic/importData";
        }
      } else if (this.type === "userWhite") {
        if (process.env.NODE_ENV !== "production") {
          return process.env.VUE_APP_URL + "/web/userWhite/importData";
          // return `${process.env.VUE_APP_URL}/web/userWhite/importData`
          // return `proxyApi/web/userWhite/importData`
        } else {
          return process.env.VUE_APP_URL + "/web/userWhite/importData";
        }
      }
    },
    headers() {
      return {
        Authorization: sessionStorage.token
      };
    }
  },
  mounted() {},
  methods: {
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    on_success(response, file, fileList) {
      this.$message({
        message: response.msg,
        type: response.code != 1 ? "success" : "error"
      });
      this.$emit("uploadFile_call", true);
    }
  }
};
</script>

<style scoped lang="scss"></style>
